package com.kstech.test.wqc.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.kstech.test.wqc.Globals;
import com.kstech.test.wqc.R;
import com.kstech.test.wqc.View.DataChangeListenner;
import com.kstech.test.wqc.View.DataFactoryThread;
import com.kstech.test.wqc.View.DataShowView;
import com.kstech.test.wqc.communication.CommunicationTask;
import com.kstech.test.wqc.listener.ViewDataChangeListener;
import com.kstech.test.wqc.utils.AppContext;

import J1939.J1939_Task;

/**
 * 实时数据显示的模块 并注册相应button点击事件
 * Created by lenovo on 2016/11/2.
 */

public class DatashowFragment extends Fragment {
    private J1939_Task j1939Task;
    private GridView gv;
    private MyAdapter adapter;
    private Button btnGet;
    private Button btnAbove;
    private StringBuffer sb = new StringBuffer();
    private RecyclerDataFragment rdf;
    private AboveFragment af = new AboveFragment();
    private LinearLayout llAbove ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppContext.initResource(getActivity());
        AppContext.loadDeviceModelFile("0001","00001001",getActivity());
        llAbove = (LinearLayout) getActivity().findViewById(R.id.ll_above);
        j1939Task= new J1939_Task();
        j1939Task.Init();
        j1939Task.start();
        CommunicationTask.start();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ttt,null);
        gv = (GridView) view.findViewById(R.id.gv_datashow);
        btnGet = (Button) view.findViewById(R.id.btn_get_data);
        btnAbove = (Button) view.findViewById(R.id.btn_show_above);
        adapter = new MyAdapter();
        gv.setAdapter(adapter);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("CLC","点击了一次");
                Globals.content.clear();
                for (int i = 0; i < Globals.values.size(); i++) {
                    DataShowView ds = Globals.values.get(i);
                    Globals.content.add(ds.getValue());
                    sb.append(ds.getItem()+"----"+ds.getValue()+"    ");
                }
                rdf = (RecyclerDataFragment) getActivity().getFragmentManager().findFragmentByTag("rdf");
                sb.append("\n");
                Log.i("CLC",sb.toString());
                rdf.update();
            }
        });
        btnAbove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if("cancel".equals(btnAbove.getText().toString())){
                    getActivity().getFragmentManager().beginTransaction().remove(af).commit();
                    llAbove.setVisibility(View.GONE);
                    btnAbove.setText("above");
                }else {
                    llAbove.setVisibility(View.VISIBLE);
                    getActivity().getFragmentManager().beginTransaction().add(R.id.ll_above,af).commit();
                    btnAbove.setText("cancel");
                }

            }
        });
        return view;
    }
    private void initData(){

    }



    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return Globals.values.size();
        }

        @Override
        public Object getItem(int i) {
            return Globals.values.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            return Globals.values.get(i);
        }
    }
}
